package git.matrix.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import git.matrix.pojo.Provider;
import git.matrix.service.ProviderServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 供应商 控制器
 * 
 * @author admin
 * 
 */
@RequestMapping("provider")
@Controller
public class ProviderController {
	@Autowired
    ProviderServices proServices;// 服务

	/**
	 * 显示供应商页面 调用服务层 --mapper 数据 ---》进行数据渲染
	 * 
	 * @return
	 */
	@RequestMapping("/providerlist")
	public String toProvider(Model model, HttpServletRequest req)
			throws Exception {
		// 获取查询queryProCode
		String queryProCode = req.getParameter("queryProCode");
		String queryProName = req.getParameter("queryProName");
		System.out.println(queryProCode + "查询供应商------------" + queryProName);

		List<Provider> data = proServices.getProByNameCode(queryProCode,
				queryProName);
		// 把数据传递 页面 model
		model.addAttribute("queryProCode", queryProCode);
		model.addAttribute("queryProName", queryProName);
		model.addAttribute("proList", data);
		// 分页显示
		return "pro/providerlist";
	}

	/**
	 * 显示 添加页面
	 * 
	 * @return
	 */
	@RequestMapping("provideradd")
	public String providerAdd() {
		return "pro/provideradd";
	}

	/**
	 * 保存供应商 取页面传递数据---服务---mapper -- 首页
	 * 
	 * @return
	 */
	public String provideraddsave(HttpServletRequest req) {
		String proCode = req.getParameter("proCode");
		String proName = req.getParameter("proName");
		String proContact = req.getParameter("proContact");
		String proPhone = req.getParameter("proPhone");
		String proAddress = req.getParameter("proAddress");
		String proFax = req.getParameter("proFax");
		String proDesc = req.getParameter("proDesc");
		// 用户输入信息
		Provider pro = new Provider();
		pro.setProCode(proCode);
		pro.setProName(proName);
		pro.setProContact(proContact);
		pro.setProPhone(proPhone);
		pro.setProAddress(proAddress);
		pro.setProFax(proFax);
		pro.setProDesc(proDesc);
		pro.setCreationDate(new Date());
		
		//调用服务层方法
		
		
		return null;//重定向供应商列表页

	}
	
	@RequestMapping("/delprovider")
	@ResponseBody
	public HashMap<String, String> delprovider(HttpServletRequest req)
	{
		int proid = Integer.valueOf(req.getParameter("proid"));//需要删除的ID
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("delResult", proServices.proDelete(proid));//还没有删除。。。
		System.out.println(map);
		return map;
		
	}
	
	///provider/proview.do?proid=
	
	
	//provider/providermodify?proid=
}
